﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ServiceModel.Syndication;

namespace etcetera.Mvc.Syndication
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SyndicationItemList<T> : IEnumerable<SyndicationItem>
    {
        private readonly IList<SyndicationItem> _syndicationItems = new List<SyndicationItem>();

        /// <summary>
        /// Initializes a new instance of the <see cref="SyndicationItemList&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <param name="titleSpecifier">The title specifier.</param>
        /// <param name="contentSpecifier">The content specifier.</param>
        /// <param name="uriSpecifier">The URI specifier.</param>
        /// <param name="idSpecifier">The id specifier.</param>
        /// <param name="dateSpecifier">The date specifier.</param>
        public SyndicationItemList(IEnumerable<T> items,
                                   Func<T, string> titleSpecifier,
                                   Func<T, string> contentSpecifier,
                                   Func<T, string> uriSpecifier,
                                   Func<T, string> idSpecifier,
                                   Func<T, DateTime> dateSpecifier)
        {
            foreach (var item in items)
            {
                string title = titleSpecifier(item);
                string content = contentSpecifier(item);
                var uri = new Uri(uriSpecifier(item));
                string id = idSpecifier(item);
                DateTime dt = dateSpecifier(item);

                SyndicationItem si = new SyndicationItem(title, content, uri, id, dt);

                _syndicationItems.Add(si);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SyndicationItemList&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <param name="titleSpecifier">The title specifier.</param>
        /// <param name="contentSpecifier">The content specifier.</param>
        /// <param name="uriSpecifier">The URI specifier.</param>
        /// <param name="idSpecifier">The id specifier.</param>
        /// <param name="dateSpecifier">The date specifier.</param>
        public SyndicationItemList(IEnumerable<T> items,
                                   Func<T, string> titleSpecifier,
                                   Func<T, string> contentSpecifier,
                                   Func<T, Uri> uriSpecifier,
                                   Func<T, string> idSpecifier,
                                   Func<T, DateTime> dateSpecifier)
        {
            foreach (var item in items)
            {
                string title = titleSpecifier(item);
                string content = contentSpecifier(item);

                var uri = uriSpecifier(item);

                string id = idSpecifier(item);
                DateTime dt = dateSpecifier(item);

                SyndicationItem si = new SyndicationItem(title, content, uri, id, dt);

                _syndicationItems.Add(si);
            }
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public IEnumerator<SyndicationItem> GetEnumerator()
        {
            return _syndicationItems.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
